package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.EventBusTags;
import com.kmproject.haifu.app.utils.CustomGridItemDecoration;
import com.kmproject.haifu.di.component.DaggerReleaseRentSeekingSelectionTypeComponent;
import com.kmproject.haifu.mvp.contract.ReleaseRentSeekingSelectionTypeContract;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.presenter.ReleaseRentSeekingSelectionTypePresenter;
import com.kmproject.haifu.mvp.ui.adapter.BaseRecyclerAdapter;
import com.kmproject.haifu.mvp.ui.holder.SmartViewHolder;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.luliang.shapeutils.DevShapeUtils.getContext;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/05/2019 15:00
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class ReleaseRentSeekingSelectionTypeActivity extends BaseActivity<ReleaseRentSeekingSelectionTypePresenter> implements ReleaseRentSeekingSelectionTypeContract.View {
    @BindView(R.id.rv_release_rent_seeking_home)
    RecyclerView rv_release_rent_seeking_home;

    private BaseRecyclerAdapter<HousePayWayBean> adapter;
    private List<HousePayWayBean> typeBeans;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerReleaseRentSeekingSelectionTypeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_release_rent_seeking_selection_type; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ArmsUtils.configRecyclerView(rv_release_rent_seeking_home, new LinearLayoutManager(getContext()));
        rv_release_rent_seeking_home.setItemAnimator(new DefaultItemAnimator());

        adapter = new BaseRecyclerAdapter<HousePayWayBean>(R.layout.item_release_rent_seeking_home) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, HousePayWayBean model, int position) {
                holder.text(R.id.tv_type_category_name, model.getName());
                RecyclerView rv = (RecyclerView) holder.findViewById(R.id.rv_release_rent_seeking_home_type);
                rv.addItemDecoration(new CustomGridItemDecoration.Builder(ReleaseRentSeekingSelectionTypeActivity.this).setVerticalSpan(0f).
                        setHorizontalSpan(1f).setColor(ContextCompat.getColor(getContext(), R.color.common_menu_text)).setShowLastLine(true).build());
                ArmsUtils.configRecyclerView(rv, new LinearLayoutManager(getContext()));
                rv.setItemAnimator(new DefaultItemAnimator());
                rv.setAdapter(new BaseRecyclerAdapter<HousePayWayBean>(R.layout.item_release_rent_seeking_home_type) {
                    @Override
                    protected void onBindViewHolder(SmartViewHolder holder, HousePayWayBean model, int position) {
                        holder.image(R.id.imv_type_icon, model.getIcon());
                        holder.text(R.id.tv_type_name, model.getName());
                        holder.text(R.id.tv_type_desc, model.getDescription());
                    }
                }
                        .refresh(model.getChildren())
                        .setOnItemClickListener((parent, view, position1, id) -> {
                            Intent intent = new Intent(ReleaseRentSeekingSelectionTypeActivity.this, ReleaseRentSeekingActivity.class);
                            HousePayWayBean item = (HousePayWayBean) ((BaseRecyclerAdapter) Objects.requireNonNull(rv.getAdapter())).getItem(position1);
                            intent.putExtra("typeName", item.getName());
//                            intent.putExtra("typeId", item.getDictId());
                            intent.putExtra("category", item.getCategory());
//                            intent.putExtra("seekingId", "1159706284341649410");
//                            intent.putParcelableArrayListExtra("typeBeans", (ArrayList<? extends Parcelable>) typeBeans);
                            ArmsUtils.startActivity(intent);
                        }));
            }
        };
        rv_release_rent_seeking_home.setAdapter(adapter);

        adapter.setOnItemClickListener((parent, view, position, id) -> {
        });
        showLoading();
        assert mPresenter != null;
        mPresenter.dictList();
    }

    @Subscriber(mode = ThreadMode.MAIN)
    public void reciverLocation(String flag) {
        if (flag.equals(EventBusTags.RELEASE_RENT_SEEKING_SUCCESS)) {
            killMyself();
        }
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void dictListSuccess(List<HousePayWayBean> housePayWayBeans) {
        hideLoading();
        typeBeans = housePayWayBeans;
        adapter.refresh(typeBeans);
    }
}
